#ifdef __APPLE__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif

#include <stdlib.h>
#include <math.h>
#include <stdbool.h>
#include <windows.h>
#include <iostream>
#include <cstdio>
#include <time.h>

#define WIDTH  800
#define HEIGHT  800

void del(int t) {
    for (int i = 0; i < t; i++)
        for (int j = 0; j < 100000; j++) { }
}

double  pi=3.141592653589;
double  dt=0.01;

double  a=0.25;
double  b=0.25;
double  g=10;
double  m=0.5;
double  alpha=40*pi/120;
double  F=100;
double  dtF=0.03;
double T=0;
double N;
double Ry, Rx;
double vx, vy;
double ax, ay;

bool was_in_air=false;

bool is_on_earth()
{
    if (Ry - b / 2 > 0) return false; else return true;
}

void move()
{
    if (is_on_earth()==false)
    {
        was_in_air=true;
        if (dtF > T)
        {
            ax=F*cos(alpha)/m;
            ay=F*sin(alpha)/m - g;
        }
        else
        {
            ay=-g;
            ax=0;
        }
    }
    else
    {
       if (was_in_air==true)
                vy=-vy*110/120;
       was_in_air=false;
       if (dtF>T) ay=F*sin(alpha)/m-g;
        else ay=-g;
       if (dtF>T) ax=F*cos(alpha);
            else ax=0;
    }
    vx-=0.05*vx;
    Rx+=vx*dt+dt*dt*ax/2;
    Ry+=vy*dt+dt*dt*ay/2;
    if (Ry<b/2) Ry=b/2;
    vy+=dt*ay; vx+=dt*ax;
}

void draw(void)
{
  glClear(GL_COLOR_BUFFER_BIT);

  glLineWidth(5);

  glColor3f(0, 0, 0.5);

  glBegin(GL_LINES);
    glVertex2f(Rx-cos(atan(b/a))*sqrt(a*a+b*b)/2, Ry+sin(atan(b/a))*sqrt(a*a+b*b)/2);
    glVertex2f(Rx+cos(atan(b/a))*sqrt(a*a+b*b)/2, Ry+sin(atan(b/a))*sqrt(a*a+b*b)/2);
  glEnd();

  glBegin(GL_LINES);
    glVertex2f(Rx+cos(atan(b/a))*sqrt(a*a+b*b)/2, Ry+sin(atan(b/a))*sqrt(a*a+b*b)/2);
    glVertex2f(Rx+cos(atan(b/a))*sqrt(a*a+b*b)/2, Ry-sin(atan(b/a))*sqrt(a*a+b*b)/2);
  glEnd();

  glBegin(GL_LINES);
    glVertex2f(Rx+cos(atan(b/a))*sqrt(a*a+b*b)/2, Ry-sin(atan(b/a))*sqrt(a*a+b*b)/2);
    glVertex2f(Rx-cos(atan(b/a))*sqrt(a*a+b*b)/2, Ry-sin(atan(b/a))*sqrt(a*a+b*b)/2);
  glEnd();

  glBegin(GL_LINES);
    glVertex2f(Rx-cos(atan(b/a))*sqrt(a*a+b*b)/2, Ry-sin(atan(b/a))*sqrt(a*a+b*b)/2);
    glVertex2f(Rx-cos(atan(b/a))*sqrt(a*a+b*b)/2, Ry+sin(atan(b/a))*sqrt(a*a+b*b)/2);
  glEnd();

  glBegin(GL_LINES);
    glVertex2f(-1, 0);
    glVertex2f(1, 0);
  glEnd();

  glFlush();
  glutSwapBuffers();
}


void timer_func(int value)
{
    del(100);
  glutPostRedisplay();
  if (Ry<b/2) dt=0;
   T+=dt;
  glutTimerFunc(dt, timer_func, 0);
  move();
}

int main(int argc, char *argv[])
{
    Rx=a/2; Ry=b/2;
    vx=0; vy=0; ax=0; ay=0;
    N=m*g;

    glutInit(&argc, argv);
    glutInitWindowSize(WIDTH, HEIGHT);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
    glutCreateWindow("Line rotation demo");

    while (((abs(vx)>0)&&(abs(vy)>0))||(Rx==a/2))
    {
    glutDisplayFunc(draw);
    glutTimerFunc(dt, timer_func, 0);
    glClearColor(0, 0.5, 0.1, 0);

    glutMainLoop();
    }
    return 0;
}
